using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AandEDLL;
using AandEDLL.DataAccess;

namespace ASA
{
    public partial class ManageUserDialog : ASA.BaseDialog
    {
        int sortedColumn = 0;
        bool bAscending = true;
        ListViewItemComparer.DataType[] columnTypes;
        User currentUser = null;

        public ManageUserDialog(User currentUser)
        {
            InitializeComponent();
            populateUserListView();
            
            // Setup column sorting
            columnTypes = new ListViewItemComparer.DataType[] {
                ListViewItemComparer.DataType.String,
                ListViewItemComparer.DataType.String,
                ListViewItemComparer.DataType.String};

            this.currentUser = currentUser;
        }

        private void populateUserListView()
        {
            lvUsers.Items.Clear();
            List<User> userList = null;
            ListViewItem item;


            try
            {
                userList = Accessor.SelectUser();
            }
            catch (Exception)
            {
                //do nothing
            }
            if (userList != null && userList.Count > 0)
            {
                foreach (User user in userList)
                {
                    DateTime compareDate = new DateTime(2007,1,1);
                    item = new ListViewItem(user.UserName);
                    //This is a hack done for time
                    if(user.LastLoginDate < compareDate)
                           item.SubItems.Add("");
                    else
                        item.SubItems.Add(user.LastLoginDate.ToShortDateString());
                    item.SubItems.Add(user.Admin.ToString());
                    item.Tag = user;
                    lvUsers.Items.Add(item);
                }
            }
        }
        private void addUser(object sender, EventArgs e)
        {
            EditUserDialog eud = new EditUserDialog();
            if (eud.ShowDialog(this) == DialogResult.OK)
            {   
                populateUserListView();
            }    
        }
        private void deleteUser(object sender, EventArgs e)
        {
            
           if (lvUsers.SelectedItems.Count > 0)
            {
               //if trying to delete admin and he is only one cannot delete
                if (Accessor.GetNumAdmins() == 1)
                {
                    if (((User)lvUsers.SelectedItems[0].Tag).Admin == true)
                    {
                        MessageBox.Show("Cannot delete.  Must have at least one Admin in the system.");
                        return;
                    }
                }
                 if (MessageBox.Show(this, "Are you sure you want to delete this user? \n" +
                  "By removing this user, all items tied to this user will be removed.",
                  "Delete User?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    return;
                try
                {
                    Accessor.DeleteUser( (User)lvUsers.SelectedItems[0].Tag );
                    if (((User)lvUsers.SelectedItems[0].Tag).UserName == currentUser.UserName)
                    {
                        MessageBox.Show(this, "The application must restart because the current \n" +
                            "user has just been deleted.", "ASA", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Application.Restart();
                    }
                }
                catch (Exception)
                {
                    //do nothing yet
                }
                populateUserListView();
            }
            else
            {
                MessageBox.Show("At least one user must be selected", "Warning");
            }
        }
        private void resetPassword(object sender, EventArgs e)
        {
            try
            {
                if (lvUsers.Items.Count > 0 && lvUsers.SelectedItems != null && lvUsers.SelectedItems.Count == 1)
                {
                    User user = (User)lvUsers.SelectedItems[0].Tag;
                    user.Password = "1234";
                    user.LastLoginDate = DateTime.Now;
                    Accessor.UpdateUser(user);

                    MessageBox.Show("Password has been reset for user '" + user.UserName + "' to '1234'");
                }
                else
                {
                    MessageBox.Show(this, "No user has been selected.", "ASA", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Error Resetting Password");
            }
        }
        private void lvUsers_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == sortedColumn)
                bAscending = !bAscending;
            else
            {
                sortedColumn = e.Column;
                bAscending = true;
            }

            lvUsers.ListViewItemSorter = new ListViewItemComparer(e.Column, bAscending, columnTypes[e.Column]);
        }

        private void updateUser(object sender, EventArgs e)
        {
            if (lvUsers.SelectedItems.Count > 0)
            {
                EditUserDialog eud = new EditUserDialog((User)lvUsers.SelectedItems[0].Tag);
                if (eud.ShowDialog(this) == DialogResult.OK)
                {
                    populateUserListView();
                }
            }
            else
            {
                MessageBox.Show("At least one user must be selected","Warning");
            }
        }
    }
}

